package com.yangrong.thread.chapter6;

import java.util.Random;

public class WriterWorker extends Thread {

	private static final Random random = new Random(System.currentTimeMillis());

	private final SharedDate data;

	private final String filler;

	private int index = 0;

	public WriterWorker(SharedDate data, String filler) {
		this.data = data;
		this.filler = filler;
	}

	@Override
	public void run() {
		try {
			while (true) {
				char c = nextChar();
				data.write(c);
				Thread.sleep(random.nextInt(1000));
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private char nextChar() {
		char c = filler.charAt(index);
		index++;
		if (index >= filler.length())
			index = 0;
		return c;

	}
}
